<template>
  <base-card :name="model.name" show-state show-btns>
    <template slot="btns">
      <el-button v-if="!editable" icon="el-icon-edit" type="text" @click="handleEdit">编辑</el-button>
      <el-button v-if="!editable && model.status === '00' && model.deadline !== undefined"
                 icon="el-icon-circle-plus-outline" type="text" @click="handleDelay">延期
      </el-button>
      <el-button v-if="!editable && model.status === '00'" icon="el-icon-circle-close"
                 type="text" @click="handleClose">注销
      </el-button>
      <el-button v-if="editable" type="text" icon="el-icon-circle-check" @click="handleSubmit">保存</el-button>
      <el-button v-if="editable" type="text" icon="el-icon-circle-close" @click="handleCancel">取消</el-button>
    </template>
    <template slot="state">
      <dict-tag :options="localDict.user_status" :value="model.status"/>
    </template>
    <section class="section">
      <el-form v-if="editable" ref="form" :model="formdata" :rules="formrules" label-suffix="：" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户账号" prop="username">
              <el-input v-model="formdata.username" :disabled="true" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户姓名" prop="name">
              <el-input v-model="formdata.name" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户类型" prop="type">
              <el-select v-model="formdata.userType" disabled placeholder="请选择用户类型" style="width: 100%">
                <el-option v-for="item in localDict.user_type" :key="item.value" :disabled="item.group==='develop'"
                           :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户状态" prop="status">
              <el-select v-model="formdata.status" disabled placeholder="请选择用户状态" style="width: 100%">
                <el-option v-for="item in localDict.user_status" :key="item.value" :label="item.label"
                           :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="mobile">
              <el-input v-model="formdata.mobile" placeholder="请输入手机号码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="formdata.email" placeholder="请输入邮箱"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任职企业" prop="companyName">
              <el-input v-model="formdata.companyName" placeholder="请输入任职企业" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号" prop="workNo">
              <el-input v-model="formdata.workNo" placeholder="请输入工号" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div v-else>
        <el-descriptions :class="['description', 'full']" :column="2"
                         :style="{width: model.avatar !== null ?'calc(100% - 150px)' : '100%', display: 'inline-block','vertical-align': 'top'}"
                         contentClassName="content"
                         labelClassName="label">
          <el-descriptions-item label="账   号">{{ model.username | formatInfo }}</el-descriptions-item>
          <el-descriptions-item label="用户类型">
            <dict-tag :options="localDict.user_type" :value="model.userType" show-label/>
            <!--          <el-tag v-if="model.gipUser" type="success" effect="dark">Gip用户</el-tag>-->
          </el-descriptions-item>
          <el-descriptions-item label="任职企业">{{ model.companyName | formatInfo }}</el-descriptions-item>
          <el-descriptions-item label="工   号">{{ model.workNo | formatInfo }}</el-descriptions-item>
          <el-descriptions-item label="手机号码">{{ model.mobile | formatInfo }}</el-descriptions-item>
          <el-descriptions-item label="电子邮件">{{ model.email | formatInfo }}</el-descriptions-item>
          <el-descriptions-item label="注册时间">{{ model.createTime | formatInfo }}</el-descriptions-item>
          <el-descriptions-item label="到期时间">{{ model.deadline | formatInfo }}</el-descriptions-item>
        </el-descriptions>
<!--        <el-image v-if="model.avatarPath !== null" :preview-src-list="[fileUrl + model.avatarPath]"-->
<!--                  :src="fileUrl + model.avatarPath" style="width: 150px; height: 150px;display: inline-block"/>-->
      </div>
    </section>
  </base-card>
</template>
<script>
import {dict, getInfo, update} from "@/api/base";
import {getStaticData} from "@/api/common";
import detailMixin from "../../../../mixins/detailMixin";

export default {
  name: 'UserDetailCard',
  dicts: [],
  props: {id: String},
  mixins: [detailMixin],
  data() {
    return {
      model: {},
      baseUrl: '/system/user/',
      selfUrl: '/self/bill/',
      fileUrl: process.env.VUE_APP_FILE_API,
      editable: false,
      formdata: {
        roleIds: []
      },
      formrules: {
        mobile: [
          {required: true, pattern: /^\d{8,15}$/, message: "请输入正确的手机号码", trigger: "blur"}
        ],
        email: [
          {required: true, type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"]}
        ],
      },
      localDict: {
        user_sex: [],
        user_type: [],
        user_status: []
      },
      dialog: {
        open: false,
        title: ''
      },
      exclusionFormRules: {
        deadline: [{required: true, message: '请选择日期', trigger: 'change'}]
      },
      exclusionFormParams: {
        deadline: ''
      },
      deadline: '',
      pickerOptions: {
        disabledDate: this.disabledDateMethod,
      }
    }
  },
  created() {
    this.getLocalDict()
    this.getModel()
  },
  computed: {},
  methods: {
    getLocalDict() {
      getStaticData('system-user-sex').then(res => {
        this.localDict.user_sex = res.data
      })
      getStaticData('system-user-type').then(res => {
        this.localDict.user_type = res.data
      })
      getStaticData('system-user-status').then(res => {
        this.localDict.user_status = res.data
      })
      dict('/system/role/', {}).then(response => {
        this.localDict.role = response.data
      })
    },
    getModel() {
      // 获取表详细信息
      getInfo(this.baseUrl, this.id).then(res => {
        this.model = res.data;
      });
    },
    handleEdit() {
      this.editable = true
      this.formdata = {...this.model}
      this.$set(this.formdata, "roleIds", this.model.roles.map(obj => obj.id));
    },
    handleDelay() {
      this.exclusionFormParams.userId = this.id
      this.deadline = this.model.deadline
      this.dialog.title = "用户延期"
      this.dialog.open = true

    },
    handleClose() {
      this.$confirm('是否确认注销该用户的访问权限？').then(() => {
        this.loading = true
        // submit(this.selfUrl, {
        //   userId: this.id,
        //   source: "01",
        //   apply: 'user_unsubscribe'
        // }).then(res => {
        //   this.$message.success('用户注销请求提交成功')
        //   this.getList()
        // }).finally(() => {
        //   this.loading = false
        //   this.nowExclusionAndUnsubscribeLoading = false
        //   this.$emit('toAuthorizeCardExclusionAndUnsubscribeLoading', false)
        // })
      }).catch(e => {
        this.$error(e.message)
      })
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          update(this.baseUrl, this.formdata).then(res => {
            this.$modal.msgSuccess("保存成功");
            this.getModel()
            this.handleCancel()
          })
        }
      });
    },
    // 取消按钮
    handleCancel() {
      this.editable = false;
      this.resetForm('form')
    },
    disabledDateMethod(time) {
      if (this.deadline) {
        return time.getTime() < new Date(this.deadline)
      } else {
        return time.getTime() < new Date('1999-01-01')
      }
    },

  }
};
</script>
<style lang="scss" scoped>

</style>
